package coupon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import coupon.domain.SkuFullReduction;
import coupon.domain.SkuLadder;
import coupon.service.MemberPriceService;
import coupon.service.SkuFullReductionService;
import coupon.mapper.SkuFullReductionMapper;
import coupon.service.SkuLadderService;
import com.feign.to.MemberPrice;
import com.feign.to.SkuReductionTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author panghu
 * @description 针对表【sms_sku_full_reduction(商品满减信息)】的数据库操作Service实现
 * @createDate 2023-03-05 14:26:58
 */
@Service
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReduction>
        implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    @Transactional
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 1、保存满减打折
        SkuLadder skuLadder = new SkuLadder();
        BeanUtils.copyProperties(skuReductionTo, skuLadder);
        skuLadder.setAddOther(skuReductionTo.getCountStatus());
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadder);
        }
        // 2、sms_sku_full_reduction 满减
        SkuFullReduction skuFullReduction = new SkuFullReduction();
        BeanUtils.copyProperties(skuReductionTo, skuFullReduction);
        if (skuFullReduction.getFullPrice().compareTo(new BigDecimal("0")) > 0) {
            this.save(skuFullReduction);
        }
        // 3、sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<coupon.domain.MemberPrice> priceList = memberPrice.stream().map(m -> {
                    coupon.domain.MemberPrice price = new coupon.domain.MemberPrice();
                    price.setSkuId(skuReductionTo.getSkuId());
                    price.setMemberPrice(m.getPrice());
                    price.setMemberLevelName(m.getName());
                    price.setAddOther(1);
                    price.setMemberLevelId(m.getId());
                    return price;
                }).filter(price -> price.getMemberPrice().compareTo(new BigDecimal("0")) > 0)
                .collect(Collectors.toList());
        memberPriceService.saveBatch(priceList);
    }
}




